<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-<%= LIQUIBASE_DTD_VERSION %>.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <%_
    const entityTableName = entity.entityTableName;
    const entityClass = entity.entityClass;
    const javadoc = entity.javadoc;
    const name = entity.name;
    if (addedFields && addedFields.length) { _%>

    <!--
        Added columns to the entity <%= entityClass %>.
    -->
    <changeSet id="<%= changelogDate %>-1-add-columns" author="jhipster">
        <addColumn tableName="<%= entityTableName %>"<%- formatAsLiquibaseRemarks(javadoc, true) %>>
            <%_ for (idx in addedFields) {
                const columnData = addedFields[idx];
              _%>
            <column name="<%= columnData.columnName %>" type="<%= columnData.columnType %>"<%- formatAsLiquibaseRemarks(columnData.javadoc, true) %>/>
                <%_ if (columnData.shouldCreateContentType) { _%>
            <column name="<%= columnData.columnName %>_content_type" type="varchar(255)"/>
                <%_ }
            } // End for (idx in addedFields) _%>
        </addColumn>
        <%_ for (idx in addedFields) {
            const columnData = addedFields[idx];
            if (columnData.fieldType === 'ZonedDateTime' || columnData.fieldType === 'Instant') { _%>
        <dropDefaultValue tableName="<%= entityTableName %>" columnName="<%= columnData.columnName %>" columnDataType="datetime"/>
        <%_ } _%>
      <%_ } _%>
    </changeSet>
    <%_ } _%>
    <%_ if (removedFields && removedFields.length) { _%>

    <!--
        Dropped columns to the entity <%= entityClass %>.
    -->
    <changeSet id="<%= changelogDate %>-1-drop-columns" author="jhipster">
        <dropColumn tableName="<%= entityTableName %>">
        <%_
        for (idx in removedFields) {
            const columnData = removedFields[idx];
        _%>
            <column name="<%= columnData.columnName %>"/>
        <%_
        } _%>
        </dropColumn>
    </changeSet>
    <%_ } _%>
    <%_ if (addedRelationships && addedRelationships.length) {
            let hasRelationShips = false;
            let hasJoinRelationShips = false;
            for (idx in addedRelationships) {
                const relationshipData = addedRelationships[idx];
                if (relationshipData.shouldWriteRelationship) {
                    hasRelationShips = true;
                } else if (relationshipData.shouldWriteJoinTable) {
                    hasJoinRelationShips = true;
                }
            }
            if (hasRelationShips || hasJoinRelationShips) {
     _%>

    <!--
        Added relationships to the entity <%= entityClass %>.
    -->
    <changeSet id="<%= changelogDate %>-1-add-relationships" author="jhipster">
        <%_ if (hasRelationShips) { _%>
        <addColumn tableName="<%= entityTableName %>"<%- formatAsLiquibaseRemarks(javadoc, true) %>>
            <%_
            for (idx in addedRelationships) {
                const relationshipData = addedRelationships[idx];
                relationshipColumnType = relationshipData.otherEntityName === 'user' && authenticationType === 'oauth2' ? 'varchar(100)' : 'bigint';
                if (relationshipData.shouldWriteRelationship) { _%>
            <column name="<%= relationshipData.columnName %>_id" type="<%= relationshipColumnType %>"/>
                <%_
                }
            } _%>
        </addColumn>
        <% } %>

 <%_     for (idx in addedRelationships) {
            const relationshipData = addedRelationships[idx];
            relationshipName = relationshipData.relationshipName,
            otherEntityName = relationshipData.otherEntityName;
            const relationshipColumnType = otherEntityName === 'user' && authenticationType === 'oauth2' ? 'varchar(100)' : 'bigint';
            if (relationshipData.shouldWriteJoinTable) {
                const joinTableName = getJoinTableName(entityTableName, relationshipName, prodDatabaseType);
_%>

        <createTable tableName="<%= joinTableName %>">
            <column name="<%= getColumnName(relationshipName) %>_id" type="<%= relationshipColumnType %>">
                <constraints nullable="false"/>
            </column>
            <column name="<%= getColumnName(name) %>_id" type="bigint">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <addPrimaryKey columnNames="<%= getColumnName(name) %>_id, <%= getColumnName(relationshipName) %>_id" tableName="<%= joinTableName %>"/>
<%              } 
            }
%>
    </changeSet>
    <%_ }
    } _%>
    <%_
/* ======== Start removed relationships exists ======== */
if (removedRelationships && removedRelationships.length) {
    let hasRelationShips = false;
    let hasJoinRelationShips = false;
    for (idx in removedRelationships) {
        const relationshipData = removedRelationships[idx];
        if (relationshipData.shouldWriteRelationship) {
            hasRelationShips = true;
        } else if (relationshipData.shouldWriteJoinTable) {
            hasJoinRelationShips = true;
        }
    }
    /* ======== Start removed relationships should be written ======== */
    if (hasRelationShips || hasJoinRelationShips) {
     _%>

    <!--
        Dropped relationships to the entity <%= entityClass %>.
    -->
    <changeSet id="<%= changelogDate %>-1-drop-relationships" author="jhipster">
        <%_
        /* ======== Start hasRelationShips ======== */
        if (hasRelationShips) {
            /* ======== Start dropForeignKeyConstraint ======== */
            for (idx in removedRelationships) {
                const relationshipData = removedRelationships[idx];
                if (relationshipData.shouldWriteRelationship) {
        _%>
        <dropForeignKeyConstraint baseTableName="<%= entityTableName %>" constraintName="<%= getFKConstraintName(entityTableName, relationshipData.relationshipName, prodDatabaseType) %>"/>
        <%_     }
            }
            /* ======== End dropForeignKeyConstraint ======== */ _%>
        <dropColumn tableName="<%= entityTableName %>">
        <%_
            /* ======== Start drop relationship column ======== */
            for (idx in removedRelationships) {
                const relationshipData = removedRelationships[idx];
                if (relationshipData.shouldWriteRelationship) { _%>
            <column name="<%= relationshipData.columnName %>_id"/>
        <%_     }
            }
            /* ======== End drop relationship column ======== */ _%>
        </dropColumn>
        <%_ }
        /* ======== End hasRelationShips ======== */ _%>
        <%_
        /* ======== Start for shouldWriteJoinTable ======== */
        for (idx in removedRelationships) {
            const relationshipData = removedRelationships[idx];
            if (relationshipData.shouldWriteJoinTable) { _%>
        <dropTable cascadeConstraints="true"
            tableName="<%= getJoinTableName(entityTableName, relationshipData.relationshipName, prodDatabaseType) %>"/>
            <%_ }
        }
        /* ======== End for shouldWriteJoinTable ======== */ _%>
    </changeSet>
    <%_
    }
    /* ======== End removed relationships should be written ======== */
}
/* ======== End removed relationships exists ======== */ _%>

    <!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

</databaseChangeLog>
